import axios from 'axios'
import Element from 'element-ui'
import router from './router'
import store from './store'


axios.defaults.baseURL = "http://119.29.28.133:8081"

// 前置拦截
axios.interceptors.request.use(config => {
    return config
})

axios.interceptors.response.use(response => {
        let res = response.data;

        console.log("=================")
        console.log(res)
        console.log("=================")

        if(res.code===201)
        {
          Element.Message.success('发送成功了！', {duration: 3 * 1000})
            return response
        }
        if(res.code===209)
        {
            Element.Message.success('注销成功', {duration: 3 * 1000})
            return response
        }
        if(res.code===206)
        {
            Element.Message.success('发送弹幕成功了！', {duration: 3 * 1000})
            return response
        }
        if(res.code===407)
        {
            Element.Message.error('请先登录！', {duration: 3 * 1000})
            return response
        }

        if(res.code===402)
        {
            Element.Message.error('发送失败,当前用户今天已发过微博,请你'+ res.data +'小时后再发', {duration: 3 * 1000})
            return response
        }

        if (res.code === 200) {

            return response
        } else {

            Element.Message.error('错了哦，这是一条错误消息', {duration: 3 * 1000})

            return Promise.reject(response.data.msg)
        }
    },
    error => {

        console.log(error)
        if(error.response.data) {
            error.message = error.response.data.msg
        }



        if(error.response.status === 401) {
            store.commit("REMOVE_INFO")
            router.push("/login")
        }

        Element.Message.error(error.message, {duration: 3 * 1000})
        return Promise.reject(error)
    }
)